package com.hongkzh.www.look.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hongkzh.www.R;
import com.hongkzh.www.look.model.bean.MyAttendBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asad on 2018/3/26.
 * 我的关注列表的Adapter
 */

public class AttSmlvAdapter extends BaseAdapter implements View.OnClickListener {

    private List<MyAttendBean.DataBean.ListBean> mAttendList = new ArrayList<>();
    private MyAttendBean.DataBean dataBean = new MyAttendBean.DataBean();

    private MyAttendBean.DataBean.ListBean mAttendBean;

    @Override
    public int getCount() {
        return mAttendList == null ? 0 : mAttendList.size();
    }

    @Override
    public Object getItem(int position) {
        return mAttendList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_attsmlv, parent, false);
            new ViewHolder(convertView);
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();
        MyAttendBean.DataBean.ListBean bean = mAttendList.get(position);
        Glide.with(parent.getContext()).load(bean.getHeadImg()).into(holder.IVAttendHead); //设置头像
        holder.TvMediaName.setText(bean.getName());
        holder.TvPeopleNum.setText(bean.getFcount() + "人已经关注");
        return convertView;
    }


    public void add(MyAttendBean attendBean) {
        if (dataBean.getPageNumber() == attendBean.getData().getPageNumber()) {
            mAttendList = attendBean.getData().getList();
        } else {
            for (int i = 0; i < attendBean.getData().getList().size(); i++) {
                mAttendList.add(attendBean.getData().getList().get(i));
            }
        }
    }

    public void remove(int position) {
        if (mAttendList != null) {
            mAttendList.remove(mAttendList.get(position));
        }
    }

    public void clearAttendList() {
        if (mAttendList != null) {
            mAttendList.clear();
        }
    }


    public MyAttendBean.DataBean.ListBean getAttendListByPosition(int pos){
        if(mAttendList!=null && mAttendList.size()!=0)
            mAttendBean=mAttendList.get(pos);
            return  mAttendBean;
    }

    @Override
    public void onClick(View v) {
        onItemClickListener.onItemClick();
    }

    public interface onItemClickListener {
        void onItemClick();
    }

    private onItemClickListener onItemClickListener;

    public void setOnItemClickListener(AttSmlvAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    static class ViewHolder {

        @BindView(R.id.IV_attend_Head)
        ImageView IVAttendHead;
        @BindView(R.id.Tv_Media_Name)
        TextView TvMediaName;
        @BindView(R.id.Tv_people_Num)
        TextView TvPeopleNum;
        @BindView(R.id.layout_Attened_item)
        RelativeLayout layoutAttenedItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setTag(this);
        }
    }

}
